/*
Copyright 2011 Christian Dadswell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package uk.co.chrisdadswell.bbcbc;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.GoogleAdView;

public class Activity_PodcastView extends Activity {
	
	final static String APP_TAG = "BBCBC";
	final static String ACT_TAG = "PodcastView";
	
	public static String podcastUrl = null;
	public static String podcastPage = null;
	public static String podcastName = null;
    public static String podcastImage =  null;
    public static String podcastDescription = null;
    public static String podcastFlavour = null;
    public static String podcastDuration  = null;
    public static String podcastGenre = null;
	private boolean listenInstalled = false;
	
	// LIFECYCLES
    @Override
	public void onStart() {
		super.onStart();
		Log.d(APP_TAG, ACT_TAG + "... OnStart ...");
	}
    
    @Override
	public void onRestart() {
		super.onRestart();
		Log.d(APP_TAG, ACT_TAG + "... OnReStart ...");
	}
	
    @Override
	public void onResume() {
		super.onResume();
		Log.d(APP_TAG, ACT_TAG + "... OnResume ...");
	}
	
    @Override
	public void onPause() {
    	 super.onPause();
    	 Log.d(APP_TAG, ACT_TAG + "... OnPause ...");
     }
	
    @Override
    public void onStop() {
    	super.onStop();
    	Log.d(APP_TAG, ACT_TAG + "... OnStop ...");
    }
     
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	Log.d(APP_TAG, ACT_TAG + "... OnDestroy ...");
    }
	
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.podcastview);
        
        // WIDGET INIT
		TextView wPodcastName = (TextView) this.findViewById(R.id.tv_podcastName);
        TextView wPodcastDuration = (TextView) this.findViewById(R.id.tv_podcastDuration);
        TextView wPodcastPage = (TextView) this.findViewById(R.id.tv_podcastPage);
        TextView wPodcastDescription = (TextView) this.findViewById(R.id.tv_podcastDescription);
        Button wbPodcastUrl = (Button)findViewById(R.id.btn_podcasturl);
        Button wbPodcastShare = (Button)findViewById(R.id.btn_podcastshare);
        
//        GoogleAdView adView = (GoogleAdView) findViewById(R.id.adview);
//        adView.showAds(adSenseSpec);
        
		Bundle intent_extras = getIntent().getExtras(); 
		if(intent_extras !=null) {
			final String selPodcast = intent_extras.getString("SelectedPodcast");
			if(GetPodcastValues(selPodcast)) {
				podcastName = selPodcast;
			}else{
				Toast.makeText(Activity_PodcastView.this, "\n\nThere was a problem retrieving podcast information for this podcast\n\n", Toast.LENGTH_LONG).show();
				finish();
			}
		}
		
		// IS LISTEN INSTALLED
		Func_FileIO.FileOrDirectoryExists(Constants.listenDir);
			if(Func_FileIO.FileOrDirectoryExists(Constants.listenDir)) {
				listenInstalled = true;
			}
		
		wbPodcastUrl.setOnClickListener(new Button.OnClickListener() {
			public void onClick (View v){ 
			    if(podcastUrl != null) {
			    	Toast.makeText(Activity_PodcastView.this, "\nSelect 'Add' to send podcast information to Google Listen\n\n" +
			    			"from there you can then Subscribe to the podcast\n", Toast.LENGTH_LONG).show();
			    	Intent intent = new Intent(Intent.ACTION_VIEW); 
					intent.setData(Uri.parse(podcastUrl));
					startActivity(intent);
			    }else{
			    	Toast.makeText(Activity_PodcastView.this, "\n\nLaunching Android Market ...\n\n", Toast.LENGTH_SHORT).show();
			    	Intent listen_intent = new Intent(Intent.ACTION_VIEW); 
					listen_intent.setData(Uri.parse("market://details?id=com.google.android.apps.listen"));
					startActivity(listen_intent);
					finish();
			    }
				}
			}
		);
		
		wbPodcastShare.setOnClickListener(new Button.OnClickListener() {
			public void onClick (View v){ 
			    if(podcastUrl != null) {
			    	Toast.makeText(Activity_PodcastView.this, "\nSelect an application to share this podcast with.\n", Toast.LENGTH_LONG).show();
					Intent share = new Intent(Intent.ACTION_SEND);
					share.setType("text/plain");
					share.putExtra(Intent.EXTRA_TEXT, podcastUrl);
					startActivity(Intent.createChooser(share, "Share Podcast Subscription"));				
			    }else{
			    	Toast.makeText(Activity_PodcastView.this, podcastUrl, Toast.LENGTH_LONG).show();
			    }
				}
			}
		);

        wPodcastName.setText(podcastName);
        wPodcastDuration.setText("Typical duration: " + podcastDuration + " mins");
        wPodcastPage.setText(podcastPage);
        wPodcastDescription.setText(podcastDescription);
	}
    
	// PREPARE OPTIONS MENU
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		menu.add(0,Constants.MENU_PREFERENCES,0,"Help / Support").setIcon(R.drawable.preferences);
		return super.onPrepareOptionsMenu(menu);
		//return true;
	}// END OF OPTIONS MENU
		
	@Override
	public boolean onOptionsItemSelected (MenuItem item){
		switch (item.getItemId()){
		case Constants.MENU_PREFERENCES:
			Intent prefsActivity = new Intent(Activity_PodcastView.this, Activity_Preferences.class);
			startActivity(prefsActivity);
			break;
		}
		return false;
	}
    
    public boolean GetPodcastValues(String podcastToGet){
    	Object[] oPInfo = null;
    	oPInfo = Xml_MainDataset.GetPodcastInfo(podcastToGet);
    	
    	if(oPInfo != null){
    		podcastUrl = oPInfo[0].toString();
    	    podcastImage = oPInfo[1].toString();
    	    podcastDescription = oPInfo[2].toString();
    	    podcastDuration = oPInfo[4].toString();
    	    podcastPage = oPInfo[5].toString();
    	    podcastGenre = oPInfo[6].toString();
    	    return true;
    	}else{
    		return false;
    	}
    }

	public void onTerminate() {
		super.onDestroy();
		this.finish();
	}
}
